<?php
/**
 * @file starsindexcontroller.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015-11-30 下午2:41:51 $
 * @brief  $
 */
namespace haibao\cms\business\control\pc\channel\stars;

use \haibao\cms\business\control\ArticleController as ArticleController;
use haibao\frame\data\query\Condition;

class StarsIndexController extends StarsController{
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}

	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		$starExample = $param = $starImg = $starImgArr = array();
		$date = new \DateTime();

		//今日明星推荐
		if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'Stars'){
			$nowDate = $date->format('m-d');
			$data = self::getStarsInstance();
			$parameters = $celebrityIdArr = array();
			if($controlData['auto']){
				foreach($controlData['auto'] as $data){
					$parameterValue = json_decode($data['ParameterValue']);
					array_push($parameters,$parameterValue);
				}
				array_slice($parameters, 0,$controlData['manual']['limit']);
			}
			$celebrityCount = count($parameters);
			if($celebrityCount>0){
				$data = self::getStarsInstance();
				foreach ($parameters as $value){
					$temp = explode(',',$value->celebrityInfo);
					array_push($celebrityIdArr, $temp['0']);
				}
				$recommendStars = $data->getRecommendStars($nowDate,$celebrityIdArr,$celebrityCount);
			}else {
				$recommendStars = $data->getRecommendStars($nowDate,'',3);
			}
			$this->assign('recommendStars', $recommendStars);
		}

		//人气明星
		if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'popularityStar'){
			$data = self::getStarsInstance();
			$popularityStar = $data->getPopularityStar();//明星人气总榜
			$modelStar = $data->getPopularityStar($starType = 3,$gender = null);//人气模特
			$popuFemaleStar = $data->getPopularityStar($starType = array(2,4),$gender = 2); //人气女明星
			$popuMaleStar = $data->getPopularityStar($starType = array(2,4),$gender = 1); //人气男明星
			$popuItGirlStar = $data->getPopularityStar($starType = 8,$gender = 2); //人气ItGirl
			$popuBabyStar = $data->getPopularityStar($starType = 10,$gender = null); //人气星宝贝
			$popuAmericaStar = $data->getPopularityStar($starType = 12,$gender = null); //人气美剧明星  原版根据美剧上升评分  暂时用明星上升评分
			$this->assign('popularityStar', $popularityStar);
			$this->assign('modelStar', $modelStar);
			$this->assign('popuFemaleStar', $popuFemaleStar);
			$this->assign('popuMaleStar', $popuMaleStar);
			$this->assign('popuItGirlStar', $popuItGirlStar);
			$this->assign('popuBabyStar', $popuBabyStar);
			$this->assign('popuAmericaStar', $popuAmericaStar);
		}

		//明星衣柜
		if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'starExample'){
			if($controlData['manual']['limit']){
				$articleControl = ArticleController::initArticleControl();
				$starExample = $articleControl->getStarExample();
				if($starExample){
					foreach($starExample as $key => $value){
					    
					    $starExample[$key]['isIncludeCn'] = $this->getIncludeCn(trim($value['BrandNameEn']));
					    $starExample[$key]['isRepeatName'] = $this->getRepeatName(trim($value['BrandNameCn']), trim($value['BrandNameEn']));
					    
						$starExample[$key]['ArticleUrl'] = $this->getArticleUrl($value['PublishId'],$value['TopTagId']['ParentId']);
						if(isset($value['BrandTopicId'][$value['BrandTagId']])){
							$starExample[$key]['StarUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STARS).'/star/'.$value['StarTopicId'][$value['StarTagId']].'/';
						}else{
							$starExample[$key]['StarUrl'] = '';
						}
						if(isset($value['BrandTopicId'][$value['BrandTagId']])){
							$starExample[$key]['BrandUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).'/brand/'.$value['BrandTopicId'][$value['BrandTagId']].'/';
						}else{
							$starExample[$key]['BrandUrl'] = '';
						}
					}
				}
			}

			$this->assign('starExample', $starExample);
		}

		//明星猜猜猜
		if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'guessStar'){
			$guessStar = $this->starsData->getArticleByTagId(self::STAR_QUESS_QUESS_QUESS);
			$guessStar['PicUrl'] = self::getImageLink($guessStar['PicUrl'],250,250);
			$guessStar['ArticleUrl'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_STAR).'/article/'.$guessStar['PublishId'].'.htm';
			$starVote = array();
			if($guessStar){
				$starVote = $this->starsData->getChoiceByArticleId($guessStar['Id']);
				if($starVote){
					$imageIdArr = $voteNum = array();
					$pattern = '/(<img\s+[^>]*>)(.*)/i';
					foreach($starVote as $key=>$val){
						if(preg_match($pattern, $val['Title'], $rel)){
							$starVote[$key]['Title'] = $rel[1];
							$starVote[$key]['Name'] = $rel[2];
						}
						$voteNum[]=$val['VoteNum'];
					}
					$voteNum = array_sum($voteNum);
					foreach($starVote as $key=>$val){
						if($voteNum != 0){
							$starVote[$key]['guessPer'] = round(($val['VoteNum']/$voteNum)*100).'%';
						}else {
							$starVote[$key]['guessPer'] = '0%';
						}
				
					}
				}
			}
			$this->assign('starVote', $starVote);
			$this->assign('guessStar', $guessStar);
		}

		//明星般配
		if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'starMached'){
			$loveStar = $this->starsData->getStarMached();//般配关系
			$num = $loveStar['Match']+$loveStar['NoMatch'];
			if($num != 0){
				$matPer = round(($loveStar['Match']/$num)*100).'%';
				$noMatPer = round(($loveStar['NoMatch']/$num)*100).'%';
			}else{
				$matPer = $noMatPer = '0%';
			}
			
			$loveStar['MatchPer'] = $matPer;
			$loveStar['NoMatchPer'] = $noMatPer;
			if($loveStar){
				$loveStarInfo = $this->starsData->getStarLoveInfo($loveStar);
				$n = 1;
				foreach($loveStarInfo as $key=>$val){
					$loveStarInfo[$key]['ImageUrl'] = $this->getImageLink($val['ImageUrl'],160,160);//明星信息
					$loveStarInfo[$key]['Sort'] = $n++;
					$loveStarInfo[$key]['StarUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STARS).$loveStarInfo[$key]['url'];
				}
			}
			$this->assign('loveStar', $loveStar);
			$this->assign('loveStarInfo', $loveStarInfo);
		}

		//精品栏目
		/**
		 * 剧好看 $starsColumn[0]
		 * 红毯点播台 $starsColumn[1]
		 * 尚道 $shangDao
		 */
		if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'refinedColumn'){
// 			if($controlData['manual']['tagId']){
// 				$starsColumn = explode(',',$controlData['manual']['tagId']);
// 			}
// 			$JuHaoKanInfo = \haibao\cms\business\control\FeatureController::getStarSerialInfoById($starsColumn[0]);
// 			if($JuHaoKanInfo && $JuHaoKanInfo->PictureId){
// 				$temp = \haibao\cms\business\ImageLibrary::getImageUrlById($JuHaoKanInfo->PictureId);
// 				$JuHaoKanInfo->PictureId = $this->getImageLink($temp[$JuHaoKanInfo->PictureId],300,225);
// 			}
// 			$HongTanInfo = \haibao\cms\business\control\FeatureController::getStarSerialInfoById($starsColumn[1]);
// 			if($HongTanInfo && $HongTanInfo->PictureId){
// 				$temp = \haibao\cms\business\ImageLibrary::getImageUrlById($HongTanInfo->PictureId);
// 				$HongTanInfo->PictureId = $this->getImageLink($temp[$HongTanInfo->PictureId],300,225);
// 			}
		    $feature = array();
		    $exclusive = \haibao\cms\business\control\FeatureController::getFeatureSerialArray();
		    if($exclusive){
		        foreach($exclusive as $key=>$exclusiveVal){
		            if(isset($exclusiveVal['children']) && !empty($exclusiveVal['children'])){
		                foreach($exclusiveVal['children'] as $k=>$val){
		                    if($exclusiveVal['Id'] == 47){
		                        $feature[] = $val['Id'];
		                    }
		                    
		                }
		            }
		        }
		    }
		    
		    $starFeature = \haibao\cms\business\control\FeatureController::getNewFeatureByChannelForIndex(0,2,false,1,$feature);

			$this->assign('starFeature', $starFeature);

		}
		$this->assign('ExclusiveUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE));
		$this->assign('BrandsUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS));
		$this->assign('StarsUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STARS));
		$this->assign('StarUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STAR));
		$this->assign('PicUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC));
		$this->assign('markType', ArticleController::INFO_TYPE_ARTICLE);
		$this->assign('attentionTypeStar', \haibao\www\model\data\Favorite::ATTENTION_TYPE_STAR);
		$this->assign('attentionTypeBrand', \haibao\www\model\data\Favorite::ATTENTION_TYPE_BRAND);
		$isNewHtml = $this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
}